<?php

function link_to($url) {
    return siteRoot . '/' . $url;
}

function translate($string) {
    return $string;
}

function sendEmail($from, $to, $subject, $message, $additionalHeaders = '') {
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";
    if ($additionalHeaders)
        $headers .= $additionalHeaders;
    return mail($to, $subject, $message, $headers);
}

function mostrarPerdido($perdido) {
//    var_dump($perdido);exit;
    $string = $perdido ? '' : 'Nenhum item perdido.';
    foreach ($perdido as $dummy) {
        foreach ($dummy as $tipo => $items) {
            $string .= "<br><b>" . $tipo . "</b>:<br><br>";
            foreach ($items as $item)
                $string .= $item . "<br>";
        }
    }
    return $string;
}

function mostrarItens($itens) {
    $string = '';
    foreach ($itens as $i => $item) {
        $string .= "<br>" . ($i + 1) . "<br><br>";
        $string .= $item . "<br>";
    }
    return $string;
}

function limparAnnote($item) {
    if (strpos($item, 'author') !== false) {
        $item = substr($item, strpos($item, 'author'));
    } else if (strpos($item, 'journal') !== false) {
        $item = substr($item, strpos($item, 'journal'));
    }
    return $item;
}

function limparAuthorJournal($item) {
    $item = preg_replace('[journal.*=.*\n]', '', $item) == null ? $item : preg_replace('[journal.*=.*\n]', '', $item);
    $item = preg_replace('[author.*=.*\n]', '', $item) == null ? $item : preg_replace('[author.*=.*\n]', '', $item);
    $item = trim($item);
    
    if (strpos($item, '{{')) {
        $posicao1 = strpos($item, '{{');
        $primeiroCaracter = 2;
    } else if (strpos($item, '{')) {
        $posicao1 = strpos($item, '{');
        $primeiroCaracter = 1;
    } else {
        header('Location: ' . link_to('home/error500'));
        exit;
    }
    
    if (strpos($item, '}}')) 
        $posicao2 = strpos($item, '}}');
    else if (strpos($item, '}')) 
        $posicao2 = strpos($item, '}');
    else {
        header('Location: ' . link_to('home/error500'));
        exit;
    }

    return substr($item, $posicao1 + $primeiroCaracter, -(strlen($item) - $posicao2));
}

function limparEspacos($item) {
    return str_replace(' ', '', $item);
}

?>
